package thinkingJava.chapter10;

interface Selector {
	boolean end();
	Object current();
	void next();
	Object ref();
}
public class Sequence {
	private Object[] items;
	private int next = 0;
	public Sequence(int size) {
		items = new Object[size];
	}
	public void add(Object o) {
		if (next < items.length) {
			items[next++] = o;
		}
	}
	private class SequenceSelector implements Selector {
		private int i = 0;
		public boolean end() {
			return i == items.length;
		}

		public Object current() {
			return items[i];
		}

		public void next() {
			if (i < items.length) {
				i++;
			}
		}
		public Sequence ref() {
			return Sequence.this;
		}
	}
	private class ReverseSelector implements Selector {
		private int i = items.length -1;
		public boolean end() {
			return i < 0;
		}

		public Object current() {
			return items[i];
		}

		public void next() {
			if (i >= 0) {
				i--;
			}
		}

		public Object ref() {
			return null;
		}

	}
	public Selector selector () {
		return new SequenceSelector();
	}
	public Selector reverseSelector() {
		return new ReverseSelector();
	}
	public static void main(String[] args) {
		Sequence sequence = new Sequence(10);
		for (int i = 0; i < 10; i++) {
			sequence.add(Integer.toString(i));
		}
		Selector selector = sequence.selector();
		while (! selector.end()) {
			System.out.print(selector.current() + "    ");
			System.out.println(selector.ref());
			selector.next();
		}
		selector = sequence.reverseSelector();
		while (! selector.end()) {
			System.out.print(selector.current() + "    ");
			selector.next();
		}
	}
}
